# Makefile for mp4v2 thumbnail extractor

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -pedantic -std=c99

# 检查C++编译器是否可用
check_cpp_compiler:
ifeq ($(OS),Windows_NT)
	@(where g++ >nul 2>nul || where c++ >nul 2>nul || where clang++ >nul 2>nul || where cl.exe >nul 2>nul) || (
		echo "错误: 未找到C++编译器! mp4v2库需要C++编译器来构建。" && 
		echo "在Windows环境下，您可以选择以下方式之一安装C++编译器:" && 
		echo "1. 安装Visual Studio (推荐): https://visualstudio.microsoft.com/" && 
		echo "2. 安装MinGW: https://www.mingw-w64.org/downloads/" && 
		echo "3. 安装MSYS2并运行: pacman -S mingw-w64-x86_64-gcc" && 
		exit 1
	)
else
	@(command -v g++ >/dev/null 2>&1 || command -v c++ >/dev/null 2>&1 || command -v clang++ >/dev/null 2>&1) || (
		echo "错误: 未找到C++编译器! mp4v2库需要C++编译器来构建。" && 
		echo "在Linux/Unix环境下，您可以使用以下命令安装C++编译器:" && 
		echo "- Ubuntu/Debian: sudo apt-get install g++" && 
		echo "- CentOS/RHEL: sudo yum install gcc-c++" && 
		echo "- Arch Linux: sudo pacman -S gcc" && 
		exit 1
	)
endif

# 检查操作系统类型
ifeq ($(OS),Windows_NT)
    # Windows环境下的设置
    # 默认库路径，可以通过MP4V2_INCLUDE和MP4V2_LIB环境变量或命令行参数覆盖
    MP4V2_INCLUDE ?= ./mp4v2_source/install/include
    MP4V2_LIB ?= ./mp4v2_source/install/lib
    INCLUDE_PATH = -I$(MP4V2_INCLUDE)
    LIBRARY_PATH = -L$(MP4V2_LIB)
    LIBS = -lmp4v2 -lz
    EXT = .exe
    # Windows下的命令替代
    MKDIR = mkdir
    RM = del /Q
    RMDIR = rmdir /S /Q
    # 检查文件存在的替代方法
    CHECK_FILE = if not exist
else
    # Linux/Unix环境下的设置
    MP4V2_INCLUDE ?= ./mp4v2_source/install/include
    MP4V2_LIB ?= ./mp4v2_source/install/lib
    INCLUDE_PATH = -I$(MP4V2_INCLUDE)
    LIBRARY_PATH = -L$(MP4V2_LIB)
    LIBS = -lmp4v2 -lz -lpthread
    EXT = 
    MKDIR = mkdir -p
    RM = rm -f
    RMDIR = rm -rf
    CHECK_FILE = if [ ! -f
endif

# 可执行文件名
TARGET = mp4v2_thumbnail$(EXT)

# 源文件
SRC = mp4v2.c

# 默认目标
all: check_cpp_compiler check_deps $(TARGET)

# 检查依赖并提示下载mp4v2库
check_deps:
ifeq ($(OS),Windows_NT)
	@$(CHECK_FILE) "$(MP4V2_INCLUDE)\mp4v2\mp4v2.h" $(RM) "$(TARGET)" && (
		echo "未找到mp4v2库头文件，开始准备mp4v2库..." && 
		$(MKDIR) mp4v2_source 2>nul && 
		chmod +x ./download_mp4v2.sh && 
		./download_mp4v2.sh && 
		@if not exist "$(MP4V2_INCLUDE)\mp4v2\mp4v2.h" (
			echo. && 
			echo "自动构建mp4v2库失败，请参考install_guide.txt手动构建mp4v2库，然后：" && 
			echo "1. 手动设置MP4V2_INCLUDE和MP4V2_LIB环境变量指向您的mp4v2库安装路径" && 
			echo "   例如: set MP4V2_INCLUDE=C:\path\to\mp4v2\include" && 
			echo "   set MP4V2_LIB=C:\path\to\mp4v2\lib" && 
			echo "2. 或者在make命令中指定：" && 
			echo "   make MP4V2_INCLUDE=C:\path\to\mp4v2\include MP4V2_LIB=C:\path\to\mp4v2\lib" && 
			exit 1
		)
	)
else
	@$(CHECK_FILE) "$(MP4V2_INCLUDE)/mp4v2/mp4v2.h" ] || [ ! -f "$(MP4V2_LIB)/libmp4v2.a" ]; then \
		echo "未找到mp4v2库，开始准备mp4v2库..."; \
		chmod +x ./download_mp4v2.sh && ./download_mp4v2.sh; \
		if [ ! -f "$(MP4V2_INCLUDE)/mp4v2/mp4v2.h" ] || [ ! -f "$(MP4V2_LIB)/libmp4v2.a" ]; then \
			echo "\n自动构建mp4v2库失败，请参考下载脚本中的说明手动构建mp4v2库，然后："; \
			echo "1. 手动设置MP4V2_INCLUDE和MP4V2_LIB环境变量指向您的mp4v2库安装路径"; \
			echo "   例如: export MP4V2_INCLUDE=/path/to/mp4v2/include"; \
			echo "   export MP4V2_LIB=/path/to/mp4v2/lib"; \
			echo "2. 或者在make命令中指定："; \
			echo "   make MP4V2_INCLUDE=/path/to/mp4v2/include MP4V2_LIB=/path/to/mp4v2/lib"; \
			exit 1; \
		fi; \
	fi
endif

# 编译目标
$(TARGET): $(SRC)
	$(CC) $(CFLAGS) $(INCLUDE_PATH) -o $@ $^ $(LIBRARY_PATH) $(LIBS)

# 清理目标文件
clean:
ifeq ($(OS),Windows_NT)
	$(RM) $(TARGET) *.o 2>nul
else
	$(RM) $(TARGET) *.o
endif

# 清理所有生成的文件，包括下载的库
clean-all:
	make clean
ifeq ($(OS),Windows_NT)
	$(RMDIR) mp4v2_source 2>nul
else
	$(RMDIR) mp4v2_source
endif

# 运行示例
run:
ifeq ($(OS),Windows_NT)
	@if exist video3.mp4 (
		./$(TARGET) video3.mp4 10 thumbnail.jpg
	) else (
		echo "未找到video3.mp4文件，请提供有效的MP4文件路径"
	)
else
	@if [ -f video3.mp4 ]; then \
		./$(TARGET) video3.mp4 10 thumbnail.jpg; \
	else \
		echo "未找到video3.mp4文件，请提供有效的MP4文件路径"; \
	fi
endif

# 显示帮助信息
help:
	@echo "使用说明："
	@echo "  make                          - 编译程序（会自动准备mp4v2库）"
	@echo "  make clean                    - 清理编译生成的文件"
	@echo "  make clean-all                - 清理所有生成的文件，包括下载的库"
	@echo "  make run                      - 运行示例（使用默认参数）"
	@echo "  make help                     - 显示帮助信息"
	@echo "  make MP4V2_INCLUDE=<path> MP4V2_LIB=<path> - 指定mp4v2库的路径"
	@echo "\n程序用法："
	@echo "  ./$(TARGET) <input_mp4_file> <time_seconds> <output_thumbnail_file>"
	@echo "\n环境变量："
	@echo "  MP4V2_INCLUDE - mp4v2库头文件所在目录"
	@echo "  MP4V2_LIB     - mp4v2库文件所在目录"
	@echo "\n注意事项："
	@echo "  1. 如果自动构建mp4v2库失败，请参考install_guide.txt手动构建"
	@echo "  2. Windows用户推荐使用Visual Studio打开vstudio9.0/mp4v2.sln来构建mp4v2库"
	@echo "  3. 构建完成后，请确保将mp4v2库的路径正确设置"